package com.cbgolf.oa.widget.pickview.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.cbgolf.oa.R;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import java.util.ArrayList;

public class OptionsPickerView<T> extends BasePickerView implements View.OnClickListener {

  WheelOptions wheelOptions;
  private View btnSubmit, btnCancel, DateView, qiuHunView;
  private TextView title1Tv, title2Tv;
  private OnOptionsSelectListener optionsSelectListener;
  private static final String TAG_SUBMIT = "submit";
  private static final String TAG_CANCEL = "cancel";
  private View optionspicker;
  private View optionspicker_QiuChang;
  private View top1;
  private View top2;

  public OptionsPickerView(Context context) {
    super(context);
    LayoutInflater.from(context).inflate(R.layout.pickerview_options, contentContainer);
    DateView = findViewById(R.id.year_m_day_ll);
    qiuHunView = findViewById(R.id.pick_pro_city_qiuchang_ll);
    // -----确定和取消按钮
    btnSubmit = findViewById(R.id.btnSubmit);
    btnSubmit.setTag(TAG_SUBMIT);
    btnCancel = findViewById(R.id.btnCancel);
    btnCancel.setTag(TAG_CANCEL);
    btnSubmit.setOnClickListener(this);
    btnCancel.setOnClickListener(this);
    //顶部标题
    title1Tv = (TextView) findViewById(R.id.tvTitle);
    title2Tv = (TextView) findViewById(R.id.top2_title_tv);

    // ----转轮
    optionspicker = findViewById(R.id.optionspicker);
    optionspicker_QiuChang = findViewById(R.id.optionspicker_qiuchang_ll);
    top1 = findViewById(R.id.pick_top_layout);
    top2 = findViewById(R.id.top_pickview2_layout);

    optionspicker.setVisibility(View.VISIBLE);
    optionspicker_QiuChang.setVisibility(View.GONE);
    qiuHunView.setVisibility(View.GONE);
    DateView.setVisibility(View.VISIBLE);
    wheelOptions = new WheelOptions(optionspicker);

  }

  public void setType(String type) {
    DateView.setVisibility(Util.isEquals("sex", type) ? View.GONE : View.VISIBLE);
  }

  public void setPicker(ArrayList<T> optionsItems) {
    wheelOptions.setPicker(optionsItems, null, null, false);
  }

  public void setPicker(ArrayList<T> options1Items,
      ArrayList<ArrayList<T>> options2Items, boolean linkage) {
    wheelOptions.setPicker(options1Items, options2Items, null, linkage);
  }

  public void setPicker(ArrayList<T> options1Items,
      ArrayList<ArrayList<T>> options2Items,
      ArrayList<ArrayList<ArrayList<T>>> options3Items,
      boolean linkage) {
    wheelOptions.setPicker(options1Items, options2Items, options3Items,
        linkage);
  }

  /**
   * 设置选中的item位置
   */
  public void setSelectOptions(int option1) {
    wheelOptions.setCurrentItems(option1, 0, 0);
  }

  /**
   * 设置选中的item位置
   */
  public void setSelectOptions(int option1, int option2) {
    wheelOptions.setCurrentItems(option1, option2, 0);
  }

  /**
   * 设置选中的item位置
   */
  public void setSelectOptions(int option1, int option2, int option3) {
    wheelOptions.setCurrentItems(option1, option2, option3);
  }

  /**
   * 设置选项的单位
   */
  public void setLabels(String label1) {
    ViewUtils.setVisible(DateView, !Util.isNull(label1));
    wheelOptions.setLabels(label1, null, null);
  }

  /**
   * 设置选项的单位
   */
  public void setLabels(String label1, String label2) {
    wheelOptions.setLabels(label1, label2, null);
  }

  /**
   * 设置选项的单位
   */
  public void setLabels(String label1, String label2, String label3) {
    wheelOptions.setLabels(label1, label2, label3);
  }

  /**
   * 设置是否循环滚动
   */
  public void setCyclic(boolean cyclic) {
    wheelOptions.setCyclic(cyclic);
  }

  public void setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3) {
    wheelOptions.setCyclic(cyclic1, cyclic2, cyclic3);
  }


  @Override
  public void onClick(View v) {
    String tag = (String) v.getTag();
    if (tag.equals(TAG_CANCEL)) {
      dismiss();
      return;
    } else {
      if (optionsSelectListener != null) {
        int[] optionsCurrentItems = wheelOptions.getCurrentItems();
        optionsSelectListener.onOptionsSelect(optionsCurrentItems[0], optionsCurrentItems[1],
            optionsCurrentItems[2]);
      }
      dismiss();
      return;
    }
  }

  public interface OnOptionsSelectListener {

    public void onOptionsSelect(int options1, int option2, int options3);
  }

  public void setOnoptionsSelectListener(
      OnOptionsSelectListener optionsSelectListener) {
    this.optionsSelectListener = optionsSelectListener;
  }

  public void setTitle(String title) {
    if (null != title1Tv) {
      title1Tv.setText(TextUtil.Text(title));
    }
    if (null != title2Tv) {
      title2Tv.setText(TextUtil.Text(title));
    }
  }
}
